package com.wwwarehouse.resource_center.fragment.maintenanceunit;


import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.bean.response.CardDeskFunctionResponseBean;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.maintenanceunit.UpdateUnitBean;
import com.wwwarehouse.resource_center.constant.ResourceConstant;
import com.wwwarehouse.resource_center.eventbus_event.UnitEvent;
import com.wwwarehouse.resource_center.fragment.base.MyBaseViewPagerFragment;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * Creator by zhenwang.xiang on 2017/10/14 11:29
 * Description: 修改单位的viewpager
 */

@Route(path = ResourceConstant.PATH_UPDATEUNIT_FRAGMENT)
public class UpdateUnitViewPagerFragment extends MyBaseViewPagerFragment {
    List<UpdateUnitBean> mInfoList = null;
    private boolean isShow = true;
    int page;
    CardDeskFunctionResponseBean.TaskBean mTaskBean;
    String ownerUkid = "";

    @Override
    public void setTitle() {
        if (peekFragment() instanceof UpdateUnitViewPagerFragment)
            mActivity.setTitle(getString(R.string.update_unit));
    }

    @Override
    public void requestDatas() {
        requestData();
    }

    private void requestData() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            mTaskBean = (CardDeskFunctionResponseBean.TaskBean) bundle.getSerializable(Constant.KEY_CARD_DESK_FUNCTION_DETAILS);
            if (mTaskBean != null) {
                ownerUkid = mTaskBean.getBusinessId();
            }
        }
        mCustomScroll.setOnPullListener(null);
        mStateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NewUnitFragment newUnitFragment = new NewUnitFragment();
                Bundle bundle = new Bundle();
                bundle.putString("ownerUkid", ownerUkid);
                bundle.putBoolean("refresh", true);
                newUnitFragment.setArguments(bundle);
                pushFragment(newUnitFragment);
            }
        });
        requestHttp();
    }

    public void requestHttp() {
        Map<String, String> map = new HashMap<>();
        map.put("ownerUkid", ownerUkid);
        httpPost(ResourceConstant.GET_UNUSED_UNIT, map, 0, false, "");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private Handler mHandler = new Handler();

    public void onEventMainThread(UnitEvent event) {
        if (event.getMsg().equals("update")) {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    requestHttp();
                }
            }, 500);
        }

    }

    @Override
    public void onReLoad(int code) {
        requestData();
    }

    @Override
    public void onUpPull() {
        if (isShow) {
            mStateButton.setVisibility(View.VISIBLE);
            mPagerInternal.setVisibility(View.GONE);
            mSlideBarView.setVisibility(View.GONE);

        } else {
            mStateButton.setVisibility(View.GONE);
            if (count <= 6) {
                mPagerInternal.setVisibility(View.VISIBLE);
            } else {
                mSlideBarView.setVisibility(View.VISIBLE);
            }
        }
        isShow = !isShow;

    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        switch (what) {
            case 0:
                mCustomScroll.setOnPullListener(this);
                if ("0".equals(commonClass.getCode())) {
                    mInfoList = JSON.parseArray(commonClass.getData().toString(), UpdateUnitBean.class);
                    if (mInfoList != null && mInfoList.size() > 0) {
                        int total = mInfoList.size();
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("infolist", (Serializable) mInfoList);
                        SetFragmentAndPage(total, 27, new UpdateUnitFragment().getClass().getName(), bundle);
                    }
                } else {
                    toast(commonClass.getMsg());
                }
                break;
        }
    }
}
